<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(value, index) in myChannels"
        :key="index"
        @click="onMyChannelClick(value, index)"
      >
        <van-icon
          slot="icon"
          name="close"
          v-show="isEdit && value.id != 0"
        ></van-icon>
        <span class="text" :class="{ active: active === index }" slot="text">{{
          value.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="推荐频道" :border="false"> </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="channel-item"
        v-for="value in recommendChannels"
        :key="value.id"
        icon="plus"
        @click="onAddChannel(value)"
      >
        <span class="text" slot="text">{{ value.name }}</span>
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/article.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storeage.js'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allChannels.filter(v => {
        return !this.myChannels.find(mv => {
          return v.id === mv.id
        })
      })
    }
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
        // console.log(this.allChannels)
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 添加事件
    async onAddChannel (item) {
      this.myChannels.push(item)
      if (this.user) {
        // 登录状态下 就发起覆盖请求 把新的数组传到服务器
        try {
          await addUserChannel({
            id: item.id,
            seq: this.myChannels.length
          })
          this.$toast('更新用户频道成功')
        } catch (err) {
          this.$toast('更新用户频道失败')
        }
      } else {
        // 未登录状态下
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    // 删除事件
    async onMyChannelClick (channelItem, i) {
      if (this.isEdit) {
        // 删除操作
        if (i === 0) return
        if (i <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.myChannels.splice(i, 1)
        this.deleteChannel(channelItem)
      } else {
        // 跳转事件
        this.$emit('update-active', i, false)
      }
    },

    // 封装删除事件
    async deleteChannel (channelItem) {
      // 登录状态
      try {
        if (this.user) {
          await deleteUserChannel(channelItem.id)
          this.$toast('删除频道成功')
        } else {
          // 未登录
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('删除频道失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .van-button {
    padding: 0 26px;
    line-height: 100%;
    margin-bottom: 15px;
  }
  .channel-item {
    height: 86px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 26px;
        white-space: nowrap;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -16px;
    font-size: 36px;
    color: #ccc;
  }
  /deep/ .recommend-grid {
    .van-grid-item__content {
      flex-direction: row;
      // color: #f00;
      padding: 0 0px;
      color: #222;
      .van-icon {
        font-size: 22px;
      }

      .van-grid-item__text,
      .text {
        font-size: 22px;
        margin-top: 0;
        white-space: nowrap;
      }
    }
  }
}
</style>
